<?php
namespace app\api\controller;

//use app\api\model\Areas;
use think\Request;

class Areas extends Base
{
    // 不需要验证
    protected $excludeValidateCheck = [
        'picker', 'lists'
    ];

    // 定义自动实例化模型
    protected $ModelPath = 'Areas';

    public function picker()
    {
        //获取各级地址
        $province_addresses = $this->M->where('parent_id', 100)->order('code', 'asc')->field(['id', 'guard_name'])->select()->toArray();
        //追加第一行信息
        array_unshift($province_addresses, ['id' => '', 'guard_name' => '请选择']);
        $city_addresses = [['id' => '', 'guard_name' => '请选择']];
        $area_addresses = [['id' => '', 'guard_name' => '请选择']];
        //整理地址数据
        $address['address_columns'] = [$province_addresses, $city_addresses, $area_addresses];
        //返回数据
        return showSuccess($address);
    }

    public function lists()
    {
        //获取父级ID
        $parent_id = (int)request()->param('id', 100);
        //查询子地区信息
        $areas = $this->M->where('parent_id', $parent_id)->order('code', 'asc')->field(['id', 'guard_name'])->select()->toArray();
        //追加第一行信息
        array_unshift($areas, ['id' => '', 'guard_name' => '请选择']);
        //发挥数据
        return showSuccess($areas);
    }

}